package com.kang.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kang.mapper.ItemsMapper;
import com.kang.pojo.Catalog;
import com.kang.pojo.Items;
import com.kang.service.CatalogService;
import com.kang.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 吃三明治派对中的康康
 * @Date 2022/4/25 19:53
 * @Version 1.0
 */

@Service
public class ItemsServiceImpl implements ItemsService {
    //默认传入显示数量
    private static final int PAGE_SIZE = 8;
    private static final int INDEX_START_PAGE = 1;

    @Autowired
    ItemsMapper itemsMapper;
    @Autowired
    CatalogService catalogService;

    @Override
    public PageInfo<Items> selectGoodsByPage(String catalogName, int start) {
        PageHelper.startPage(start,PAGE_SIZE);
        return new PageInfo<>(itemsMapper.searchAllByCatalog(catalogName));
    }

    @Override
    public List<Items> indexGoods() {
        ArrayList<Items> itemsArrayList = new ArrayList<>();
        List<Catalog> catalogs = catalogService.allCatalog();
        for (Catalog catalog : catalogs) {
            //如果查询不为空
            if(itemsMapper.searchIndexItems(catalog.getCatalog()) != null){
                itemsArrayList.addAll(itemsMapper.searchIndexItems(catalog.getCatalog()));
            }
        }
        return itemsArrayList;
    }

    @Override
    public Items selectGoodsById(long id) {

        return itemsMapper.selectByPrimaryKey(id);


    }

}
